package com.yuexunit.operationman.nbsy.module.nurse.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;

import com.yuexunit.operationman.nbsy.R;
import com.yuexunit.operationman.nbsy.base.BaseKJActivity;
import com.yuexunit.operationman.nbsy.base.ExitApplication;
import com.yuexunit.operationman.nbsy.base.SubmitBean;
import com.yuexunit.operationman.nbsy.config.AppConfig;
import com.yuexunit.operationman.nbsy.module.InterventionalOperation.Bean.DrugBean;
import com.yuexunit.operationman.nbsy.module.InterventionalOperation.adapter.OtherListAdapter;
import com.yuexunit.operationman.nbsy.module.SafeOperation.bean.SignBean;
import com.yuexunit.operationman.nbsy.module.SafeOperation.model.impl.SafeOperationSignModel;
import com.yuexunit.operationman.nbsy.module.SafeOperation.presenter.ISafeOperationSignPresenter;
import com.yuexunit.operationman.nbsy.module.SafeOperation.presenter.impl.SafeOperationSignPresenter;
import com.yuexunit.operationman.nbsy.module.SafeOperation.ui.view.ISafeOperationSignView;
import com.yuexunit.operationman.nbsy.module.nurse.Bean.NurseBean;
import com.yuexunit.operationman.nbsy.module.nurse.model.impl.NurseModel;
import com.yuexunit.operationman.nbsy.module.nurse.presenter.INursePresenter;
import com.yuexunit.operationman.nbsy.module.nurse.presenter.impl.NursePresenter;
import com.yuexunit.operationman.nbsy.module.nurse.ui.view.INurseView;
import com.yuexunit.operationman.nbsy.widget.ActionSheetDialog;
import com.yuexunit.operationman.nbsy.widget.ClickDialog;
import com.yuexunit.operationman.nbsy.widget.CustomDatePicker;
import com.yuexunit.operationman.nbsy.widget.FixedSwipeListView;
import com.yuexunit.operationman.nbsy.widget.LoadingDialog;
import com.yuexunit.operationman.nbsy.widget.LoginDialog;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by chenss on 2017/7/7 0007.
 */

public class Act_Ops_Pre extends BaseKJActivity implements ISafeOperationSignView, INurseView {
    @Bind(R.id.tv_patient_name)
    TextView tvPatientName;
    @Bind(R.id.tv_patient_sex)
    TextView tvPatientSex;
    @Bind(R.id.tv_patient_visitno)
    TextView tvPatientVisitno;
    @Bind(R.id.tv_patient_disease)
    TextView tvPatientDisease;
    @Bind(R.id.join_date)
    TextView joinDate;
    @Bind(R.id.ops_pre_checkbox_01_01)
    CheckBox opsPreCheckbox0101;
    @Bind(R.id.ops_pre_checkbox_01_02)
    CheckBox opsPreCheckbox0102;
    @Bind(R.id.ops_pre_checkbox_02_01)
    CheckBox opsPreCheckbox0201;
    @Bind(R.id.ops_pre_checkbox_02_02)
    CheckBox opsPreCheckbox0202;
    @Bind(R.id.ops_pre_checkbox_03_01)
    CheckBox opsPreCheckbox0301;
    @Bind(R.id.ops_pre_checkbox_03_02)
    CheckBox opsPreCheckbox0302;
    @Bind(R.id.ops_pre_checkbox_03_03)
    CheckBox opsPreCheckbox0303;
    @Bind(R.id.ops_pre_checkbox_04_01)
    CheckBox opsPreCheckbox0401;
    @Bind(R.id.ops_pre_checkbox_04_02)
    CheckBox opsPreCheckbox0402;
    @Bind(R.id.ops_pre_checkbox_05_01)
    CheckBox opsPreCheckbox0501;
    @Bind(R.id.ops_pre_checkbox_05_02)
    CheckBox opsPreCheckbox0502;
    @Bind(R.id.ops_pre_checkbox_06_01)
    CheckBox opsPreCheckbox0601;
    @Bind(R.id.ops_pre_checkbox_06_02)
    CheckBox opsPreCheckbox0602;
    @Bind(R.id.ops_pre_checkbox_07_01)
    CheckBox opsPreCheckbox0701;
    @Bind(R.id.ops_pre_checkbox_07_02)
    CheckBox opsPreCheckbox0702;
    @Bind(R.id.ops_pre_checkbox_08_01)
    CheckBox opsPreCheckbox0801;
    @Bind(R.id.ops_pre_checkbox_08_02)
    CheckBox opsPreCheckbox0802;
    @Bind(R.id.skin_other_txt)
    EditText skinOtherTxt;
    @Bind(R.id.iv_skin_other)
    LinearLayout ivSkinOther;
    @Bind(R.id.ops_pre_checkbox_09_01)
    CheckBox opsPreCheckbox0901;
    @Bind(R.id.ops_pre_checkbox_09_02)
    CheckBox opsPreCheckbox0902;
    @Bind(R.id.drug_found_txt)
    EditText drugFoundTxt;
    @Bind(R.id.iv_drug_found)
    LinearLayout ivDrugFound;
    //    @Bind(R.id.fungi_txt)
//    EditText fungiTxt;
    @Bind(R.id.fungi_01)
    CheckBox fungi01;
    @Bind(R.id.sign_txt)
    EditText signTxt;
    @Bind(R.id.surgery_name)
    EditText surgeryName;
    @Bind(R.id.analgesia_listview)
    FixedSwipeListView analgesiaListview;

    //签名
    @Bind(R.id.img_ra_sign_01)
    ImageView imgRaSign01;
    @Bind(R.id.btn_ra_sign_01)
    TextView btnRaSign01;
    @Bind(R.id.tv_ra_sign_01_time)
    TextView tvRaSign01Time;
    @Bind(R.id.tv_ra_sign_01_name)
    TextView tvRaSign01Name;

    @Bind(R.id.img_ra_sign_04)
    ImageView imgRaSign04;
    @Bind(R.id.btn_ra_sign_04)
    TextView btnRaSign04;
    @Bind(R.id.tv_ra_sign_04_time)
    TextView tvRaSign04Time;
    @Bind(R.id.tv_ra_sign_04_name)
    TextView tvRaSign04Name;

    LoginDialog loginDialog;
    LoadingDialog loadingDialog;
    INursePresenter presenter;
    ISafeOperationSignPresenter presenter_sign;
    ArrayList<DrugBean> OtherDrugBeans = new ArrayList<>();
    OtherListAdapter otherListAdapter;

    private int type = -1;

    ClickDialog dialog;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_ops_pre);
        ButterKnife.bind(this);
        ExitApplication.getInstance().addActivity(this);
    }

    @Override
    public void initData() {
        super.initData();
        loginDialog = new LoginDialog(aty);
        loadingDialog = new LoadingDialog(this);
        presenter = new NursePresenter(this, new NurseModel());
        presenter_sign = new SafeOperationSignPresenter(this, new SafeOperationSignModel());
        setTitle();
        setContent();

        analgesiaListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(aty, Act_Nursing_WJB.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("drug", OtherDrugBeans.get(i));
                bundle.putInt("type", 2);
                bundle.putInt("position", i);
                intent.putExtras(bundle);
                aty.startActivityForResult(intent, 1);
            }
        });
    }

    private void setTitle() {
        tvPatientName.setText(AppConfig.NURSEBEAN.getObject().getNursing().getPatientName());
        if (AppConfig.NURSEBEAN.getObject().getNursing().getPatientSex().equals("1")) {
            tvPatientSex.setText("男");
        } else if (AppConfig.NURSEBEAN.getObject().getNursing().getPatientSex().equals("2")) {
            tvPatientSex.setText("女");
        } else {
            tvPatientSex.setText("不详");
        }
        tvPatientVisitno.setText("住院号" + AppConfig.NURSEBEAN.getObject().getNursing().getInHospitalNo());
        tvPatientDisease.setText(AppConfig.NURSEBEAN.getObject().getNursing().getOperationName());
    }

    private void setContent() {
        for (NurseBean.ObjectEntity.NursingItemListEntity p : AppConfig.NURSEBEAN.getObject().getNursingItemList()) {
            switch (p.getItemCode()) {
                case "BP_Time":
                    joinDate.setText(p.getItemValue());
                    break;
                case "BP_ShouShuShi":
                    surgeryName.setText(p.getItemValue());
                    break;
                case "BP_BingRenHeDui":
                    if (p.getItemValue().length() > 0) {
                        String[] skin = p.getItemValue().split(";", -1);
                        if (skin[0].equals("1")) {
                            opsPreCheckbox0101.setChecked(true);
                        } else if (skin[1].equals("1")) {
                            opsPreCheckbox0102.setChecked(true);
                        }
                    }
                    break;
                case "BP_ShouShuBuWeiHeDui":
                    if (p.getItemValue().length() > 0) {
                        String[] skin = p.getItemValue().split(";", -1);
                        if (skin[0].equals("1")) {
                            opsPreCheckbox0201.setChecked(true);
                        } else if (skin[1].equals("1")) {
                            opsPreCheckbox0202.setChecked(true);
                        }
                    }
                    break;
                case "BP_ShenZhi":
                    if (p.getItemValue().length() > 0) {
                        String[] skin = p.getItemValue().split(";", -1);
                        if (skin[0].equals("1")) {
                            opsPreCheckbox0301.setChecked(true);
                        }
                        if (skin[1].equals("1")) {
                            opsPreCheckbox0302.setChecked(true);
                        }
                        if (skin[2].equals("1")) {
                            opsPreCheckbox0303.setChecked(true);
                        }
                    }
                    break;
                case "BP_ShenJingMaiChuanCi":
                    if (p.getItemValue().length() > 0) {
                        String[] skin = p.getItemValue().split(";", -1);
                        if (skin[0].equals("1")) {
                            opsPreCheckbox0401.setChecked(true);
                        } else if (skin[1].equals("1")) {
                            opsPreCheckbox0402.setChecked(true);
                        }
                    }
                    break;
                case "BP_JingMaiShuYe":
                    if (p.getItemValue().length() > 0) {
                        String[] skin = p.getItemValue().split(";", -1);
                        if (skin[0].equals("1")) {
                            opsPreCheckbox0501.setChecked(true);
                        } else if (skin[1].equals("1")) {
                            opsPreCheckbox0502.setChecked(true);
                        }
                    }
                    break;
                case "BP_WeiGuan":
                    if (p.getItemValue().length() > 0) {
                        String[] skin = p.getItemValue().split(";", -1);
                        if (skin[0].equals("1")) {
                            opsPreCheckbox0601.setChecked(true);
                        } else if (skin[1].equals("1")) {
                            opsPreCheckbox0602.setChecked(true);
                        }
                    }
                    break;
                case "BP_DaoNiaoGuan":
                    if (p.getItemValue().length() > 0) {
                        String[] skin = p.getItemValue().split(";", -1);
                        if (skin[0].equals("1")) {
                            opsPreCheckbox0701.setChecked(true);
                        } else if (skin[1].equals("1")) {
                            opsPreCheckbox0702.setChecked(true);
                        }
                    }
                    break;
                case "BP_Skin":
                    if (p.getItemValue().equals("1")) {
                        opsPreCheckbox0801.setChecked(true);
                    } else if (p.getItemValue().equals("0")) {
                        opsPreCheckbox0802.setChecked(true);
                        ivSkinOther.setVisibility(View.VISIBLE);
                        skinOtherTxt.setText(p.getItemOther());
                    }
                    break;
                case "BP_YaoWuGuoMing":
                    if (p.getItemValue().length() > 0) {
                        String[] skin = p.getItemValue().split(";", -1);
                        if (skin[0].equals("1")) {
                            opsPreCheckbox0901.setChecked(true);
                        } else if (skin[1].equals("1")) {
                            opsPreCheckbox0902.setChecked(true);
                            ivDrugFound.setVisibility(View.VISIBLE);
                            drugFoundTxt.setText(skin[2]);
                        }
                    }
                    break;
                case "AP_JunBao":
                    if (p.getItemOther().length() > 0) {
                        String[] medicine = p.getItemOther().split(";", -1);

                        for (int i = 0; i < medicine.length; i++) {
                            DrugBean drugBean = new DrugBean();
                            drugBean.setName(medicine[i]);
                            OtherDrugBeans.add(drugBean);
                        }

                        otherListAdapter = new OtherListAdapter(analgesiaListview, OtherDrugBeans, R.layout.item_other_list);
                        analgesiaListview.setAdapter(otherListAdapter);
                    }
                    break;
                case "AP_JunBaoJianCe":
                    if (p.getItemValue().length() > 0) {
                        if (p.getItemValue().equals("1")) {
                            fungi01.setChecked(true);
                        }
                    }
                    break;
//                case "AP_Sign":
//                    if (p.getItemOther().length() > 0) {
//                        signTxt.setText(p.getItemOther());
//                    }
//                    break;
                case "WJB_Doctor_Sign":
                    if (p.getItemValue().length() > 0) {
                        imgRaSign01.setVisibility(View.VISIBLE);
                        btnRaSign01.setVisibility(View.GONE);
                        tvRaSign01Time.setVisibility(View.VISIBLE);
                        tvRaSign01Name.setVisibility(View.VISIBLE);
                        Glide.with(aty)
                                .load(p.getItemValue())
                                .placeholder(R.color.white)
                                .error(R.color.white)
                                .into(imgRaSign01);
                        tvRaSign01Time.setText("签名时间:" + p.getItemOther());
                        tvRaSign01Name.setText(p.getItemName());
                    }
                    break;
                case "WJB_Nurse_Sign":
                    if (p.getItemValue().length() > 0) {
                        imgRaSign04.setVisibility(View.VISIBLE);
                        btnRaSign04.setVisibility(View.GONE);
                        tvRaSign04Time.setVisibility(View.VISIBLE);
                        tvRaSign04Name.setVisibility(View.VISIBLE);
                        Glide.with(aty)
                                .load(p.getItemValue())
                                .placeholder(R.color.white)
                                .error(R.color.white)
                                .into(imgRaSign04);
                        tvRaSign04Time.setText("签名时间:" + p.getItemOther());
                        tvRaSign04Name.setText(p.getItemName());
                    }
                    break;
            }
        }
    }


    private void insertContent() {
        for (int i = 0; i < AppConfig.NURSEBEAN.getObject().getNursingItemList().size(); i++) {
            String value = "";
            switch (AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).getItemCode()) {
                case "BP_Time":
                    AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue(joinDate.getText().toString());
                    break;
                case "BP_ShouShuShi":
                    AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue(surgeryName.getText().toString());
                    break;
                case "BP_BingRenHeDui":
                    if (opsPreCheckbox0101.isChecked()) {
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue("1;");
                    }
                    if (opsPreCheckbox0102.isChecked()) {
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue(";1");
                    }
                    break;
                case "BP_ShouShuBuWeiHeDui":
                    if (opsPreCheckbox0201.isChecked()) {
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue("1;");
                    }
                    if (opsPreCheckbox0202.isChecked()) {
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue(";1");
                    }
                    break;
                case "BP_ShenZhi":
                    if (opsPreCheckbox0301.isChecked()) {
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue("1;;");
                    }
                    if (opsPreCheckbox0302.isChecked()) {
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue(";1;");
                    }
                    if (opsPreCheckbox0303.isChecked()) {
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue(";;1");
                    }
                    break;
                case "BP_ShenJingMaiChuanCi":
                    if (opsPreCheckbox0401.isChecked()) {
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue("1;");
                    }
                    if (opsPreCheckbox0402.isChecked()) {
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue(";1");
                    }
                    break;
                case "BP_JingMaiShuYe":
                    if (opsPreCheckbox0501.isChecked()) {
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue("1;");
                    }
                    if (opsPreCheckbox0502.isChecked()) {
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue(";1");
                    }
                    break;
                case "BP_WeiGuan":
                    if (opsPreCheckbox0601.isChecked()) {
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue("1;");
                    }
                    if (opsPreCheckbox0602.isChecked()) {
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue(";1");
                    }
                    break;
                case "BP_DaoNiaoGuan":
                    if (opsPreCheckbox0701.isChecked()) {
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue("1;");
                    }
                    if (opsPreCheckbox0702.isChecked()) {
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue(";1");
                    }
                    break;
                case "BP_Skin":
                    if (opsPreCheckbox0801.isChecked()) {
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue("1");
                    }
                    if (opsPreCheckbox0802.isChecked()) {
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue("0");
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemOther(skinOtherTxt.getText().toString());
                    }
                    break;
                case "BP_YaoWuGuoMing":
                    if (opsPreCheckbox0901.isChecked()) {
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue("1;;");
                    }
                    if (opsPreCheckbox0902.isChecked()) {
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue(";1;" + drugFoundTxt.getText().toString());
                    }
                    break;
                case "AP_JunBao":
                    if (OtherDrugBeans.size() > 0) {
                        String medicine = "";
                        for (int j = 0; j < OtherDrugBeans.size(); j++) {
                            if (medicine.length() > 0) {
                                medicine += ";" + OtherDrugBeans.get(j).getName();
                            } else {
                                medicine += OtherDrugBeans.get(j).getName();
                            }
                        }
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemOther(medicine);
                    } else if (OtherDrugBeans.size() == 0) {
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemOther("");
                    }
                    break;
                case "AP_JunBaoJianCe":
                    if (fungi01.isChecked()) {
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue("1");
                    }
                    break;
//                case "AP_Sign":
//                    if (fungi01.isChecked()) {
//                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemOther(signTxt.getText().toString());
//                    }
//                    break;
            }
        }

        if (AppConfig.NURSEBEAN.getObject().getNursing().getNursingId() == null) {
            Map<String, String> params = new HashMap<>();
            params.put("operationNo", PreferenceHelper.readString(aty, AppConfig.PATIENT_INFO, AppConfig.PATIENT_OPERATION_ID) + "");
            params.put("saveData", JSON.toJSONString(AppConfig.NURSEBEAN));
            presenter.insertNurseDate(params);
        } else {
            Map<String, String> params = new HashMap<>();
            params.put("accountId", PreferenceHelper.readInt(aty, AppConfig.USER_INFO, AppConfig.USER_ACCOUNT) + "");
            params.put("fullName", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.USER_FULLNAME) + "");
            params.put("saveData", JSON.toJSONString(AppConfig.NURSEBEAN));
            presenter.updateNurseDate(params);
        }
    }

    private void initDatePicker() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        Date date1 = null;
        try {
            if (joinDate.getText().toString().length() > 0) {
                date1 = sdf.parse(joinDate.getText().toString());
            } else {
                date1 = new Date();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String now = sdf.format(date1);

        CustomDatePicker customDatePicker1 = new CustomDatePicker(this, new CustomDatePicker.ResultHandler() {
            @Override
            public void handle(String time) { // 回调接口，获得选中的时间
                joinDate.setText(time);
            }
        }, "2010-01-01 00:00", now); // 初始化日期格式请用：yyyy-MM-dd HH:mm，否则不能正常运行
        customDatePicker1.showSpecificTime(true); // 显示时和分
        customDatePicker1.setIsLoop(true); // 循环滚动
        customDatePicker1.show(now);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case 20000:
                if (data.getExtras().getInt("position") == -1) {
                    DrugBean drug = (DrugBean) data.getExtras().getSerializable("drug");
                    OtherDrugBeans.add(drug);
                    if (otherListAdapter != null) {
                        otherListAdapter.notifyDataSetChanged();
                    } else {
                        otherListAdapter = new OtherListAdapter(analgesiaListview, OtherDrugBeans, R.layout.item_other_list);
                        analgesiaListview.setAdapter(otherListAdapter);
                    }
                } else {
                    DrugBean drug = (DrugBean) data.getExtras().getSerializable("drug");
                    OtherDrugBeans.set(data.getExtras().getInt("position"), drug);
                    if (otherListAdapter != null) {
                        otherListAdapter.notifyDataSetChanged();
                    } else {
                        otherListAdapter = new OtherListAdapter(analgesiaListview, OtherDrugBeans, R.layout.item_other_list);
                        analgesiaListview.setAdapter(otherListAdapter);
                    }
                }
                break;
            default:
                break;
        }
    }


    @Override
    public void insertNurseDate(SubmitBean submitBean) {
        if (submitBean.getCode() == 200) {
            ViewInject.toast("保存成功");
            finish();
        } else {
            ViewInject.toast(submitBean.getDesc());
        }
    }

    @Override
    public void updateNurseDate(SubmitBean submitBean) {
        if (submitBean.getCode() == 200) {
            ViewInject.toast("保存成功");
            finish();
        } else {
            ViewInject.toast(submitBean.getDesc());
        }
    }

    @Override
    public void loadingDialogStatus(int status) {
        switch (status) {
            case AppConfig.SHOW_DIALOG:
                loadingDialog.setCanceledOnTouchOutside(false);
                loadingDialog.show();
                break;
            case AppConfig.DISMISS_DIALOG:
                loadingDialog.dismiss();
                break;

        }
    }

    @Override
    public void getSignInfo(SignBean signBean) {
        loginDialog.dismiss();
        if (signBean.getCode() == 200) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
            Date curDate = new Date(System.currentTimeMillis());//获取当前时间
            String str = formatter.format(curDate);
            switch (type) {
                case 1:
                    if (signBean.getObject().getDoctorType().equals("Y")) {
                        imgRaSign01.setVisibility(View.VISIBLE);
                        Glide.with(this)
                                .load(signBean.getObject().getSignPicture())
                                .placeholder(R.color.white)
                                .error(R.color.white)
                                .into(imgRaSign01);
                        btnRaSign01.setVisibility(View.GONE);
                        tvRaSign01Time.setVisibility(View.VISIBLE);
                        tvRaSign01Time.setText("签名时间:" + str);
                        tvRaSign01Name.setVisibility(View.VISIBLE);
                        tvRaSign01Name.setText(signBean.getObject().getDoctorName());

                        for (int i = 0; i < AppConfig.NURSEBEAN.getObject().getNursingItemList().size(); i++) {
                            if (AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).getItemCode().equals("WJB_Doctor_Sign")) {
                                AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue(signBean.getObject().getSignPicture());
                                AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemName(signBean.getObject().getDoctorName());
                                AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemOther(str);
                            }
                        }
                    } else {
                        ViewInject.toast("请使用医生账号进行签名");
                    }
                    break;
                case 4:
                    if (signBean.getObject().getDoctorType().equals("H")) {
                        imgRaSign04.setVisibility(View.VISIBLE);
                        Glide.with(this)
                                .load(signBean.getObject().getSignPicture())
                                .placeholder(R.color.white)
                                .error(R.color.white)
                                .into(imgRaSign04);
                        btnRaSign04.setVisibility(View.GONE);
                        tvRaSign04Time.setVisibility(View.VISIBLE);
                        tvRaSign04Time.setText("签名时间:" + str);
                        tvRaSign04Name.setVisibility(View.VISIBLE);
                        tvRaSign04Name.setText(signBean.getObject().getDoctorName());

                        for (int i = 0; i < AppConfig.NURSEBEAN.getObject().getNursingItemList().size(); i++) {
                            if (AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).getItemCode().equals("WJB_Nurse_Sign")) {
                                AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue(signBean.getObject().getSignPicture());
                                AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemName(signBean.getObject().getDoctorName());
                                AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemOther(str);
                            }
                        }
                    } else {
                        ViewInject.toast("请使用护士账号进行签名");
                    }
                    break;
            }
        } else {
            ViewInject.toast(signBean.getDesc());
        }
    }

    /**
     * 通过dispatchTouchEvent每次ACTION_DOWN事件中动态判断非EditText本身区域的点击事件，然后在事件中进行屏蔽。然后当点击不是EditText时候，进行隐藏键盘操作
     *
     * @param ev
     * @return
     */

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideInput(v, ev)) {

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
            return super.dispatchTouchEvent(ev);
        }
        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);
    }

    public boolean isShouldHideInput(View v, MotionEvent event) {

        if (v != null && (v instanceof EditText)) {
            int[] leftTop = {0, 0};
            //获取输入框当前的location位置
            v.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击的是输入框区域，保留点击EditText的事件\
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

    @OnClick({R.id.titlebar_docadvice_img_back, R.id.iv_join_date, R.id.ops_pre_checkbox_01_01,
            R.id.ops_pre_checkbox_01_02, R.id.ops_pre_checkbox_02_01,
            R.id.ops_pre_checkbox_02_02, R.id.ops_pre_checkbox_03_01,
            R.id.ops_pre_checkbox_03_02, R.id.ops_pre_checkbox_03_03,
            R.id.ops_pre_checkbox_04_01, R.id.ops_pre_checkbox_04_02,
            R.id.ops_pre_checkbox_05_01, R.id.ops_pre_checkbox_05_02,
            R.id.ops_pre_checkbox_06_01, R.id.ops_pre_checkbox_06_02,
            R.id.ops_pre_checkbox_07_01, R.id.ops_pre_checkbox_07_02,
            R.id.ops_pre_checkbox_08_01, R.id.ops_pre_checkbox_08_02,
            R.id.ops_pre_checkbox_09_01, R.id.ops_pre_checkbox_09_02,
            R.id.sure, R.id.bt_add_analgesia, R.id.btn_ra_sign_01, R.id.btn_ra_sign_04})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.titlebar_docadvice_img_back:
                finish();
                break;
            case R.id.iv_join_date:
                initDatePicker();
                break;
            case R.id.ops_pre_checkbox_01_01:
                if (opsPreCheckbox0102.isChecked()) {
                    opsPreCheckbox0102.setChecked(false);
                }
                break;
            case R.id.ops_pre_checkbox_01_02:
                if (opsPreCheckbox0101.isChecked()) {
                    opsPreCheckbox0101.setChecked(false);
                }
                break;
            case R.id.ops_pre_checkbox_02_01:
                if (opsPreCheckbox0202.isChecked()) {
                    opsPreCheckbox0202.setChecked(false);
                }
                break;
            case R.id.ops_pre_checkbox_02_02:
                if (opsPreCheckbox0201.isChecked()) {
                    opsPreCheckbox0201.setChecked(false);
                }
                break;
            case R.id.ops_pre_checkbox_03_01:
                if (opsPreCheckbox0302.isChecked()) {
                    opsPreCheckbox0302.setChecked(false);
                }
                if (opsPreCheckbox0303.isChecked()) {
                    opsPreCheckbox0303.setChecked(false);
                }
                break;
            case R.id.ops_pre_checkbox_03_02:
                if (opsPreCheckbox0301.isChecked()) {
                    opsPreCheckbox0301.setChecked(false);
                }
                if (opsPreCheckbox0303.isChecked()) {
                    opsPreCheckbox0303.setChecked(false);
                }
                break;
            case R.id.ops_pre_checkbox_03_03:
                if (opsPreCheckbox0301.isChecked()) {
                    opsPreCheckbox0301.setChecked(false);
                }
                if (opsPreCheckbox0302.isChecked()) {
                    opsPreCheckbox0302.setChecked(false);
                }
                break;
            case R.id.ops_pre_checkbox_04_01:
                if (opsPreCheckbox0402.isChecked()) {
                    opsPreCheckbox0402.setChecked(false);
                }
                break;
            case R.id.ops_pre_checkbox_04_02:
                if (opsPreCheckbox0401.isChecked()) {
                    opsPreCheckbox0401.setChecked(false);
                }
                break;
            case R.id.ops_pre_checkbox_05_01:
                if (opsPreCheckbox0502.isChecked()) {
                    opsPreCheckbox0502.setChecked(false);
                }
                break;
            case R.id.ops_pre_checkbox_05_02:
                if (opsPreCheckbox0501.isChecked()) {
                    opsPreCheckbox0501.setChecked(false);
                }
                break;
            case R.id.ops_pre_checkbox_06_01:
                if (opsPreCheckbox0602.isChecked()) {
                    opsPreCheckbox0602.setChecked(false);
                }
                break;
            case R.id.ops_pre_checkbox_06_02:
                if (opsPreCheckbox0601.isChecked()) {
                    opsPreCheckbox0601.setChecked(false);
                }
                break;
            case R.id.ops_pre_checkbox_07_01:
                if (opsPreCheckbox0702.isChecked()) {
                    opsPreCheckbox0702.setChecked(false);
                }
                break;
            case R.id.ops_pre_checkbox_07_02:
                if (opsPreCheckbox0701.isChecked()) {
                    opsPreCheckbox0701.setChecked(false);
                }
                break;
            case R.id.ops_pre_checkbox_08_01:
                if (opsPreCheckbox0802.isChecked()) {
                    opsPreCheckbox0802.setChecked(false);
                    ivSkinOther.setVisibility(View.GONE);
                }
                break;
            case R.id.ops_pre_checkbox_08_02:
                if (opsPreCheckbox0801.isChecked()) {
                    opsPreCheckbox0801.setChecked(false);
                }
                if (opsPreCheckbox0802.isChecked()) {
                    ivSkinOther.setVisibility(View.VISIBLE);
                } else {
                    ivSkinOther.setVisibility(View.GONE);
                }
                break;
            case R.id.ops_pre_checkbox_09_01:
                if (opsPreCheckbox0902.isChecked()) {
                    opsPreCheckbox0902.setChecked(false);
                    ivDrugFound.setVisibility(View.GONE);
                }
                break;
            case R.id.ops_pre_checkbox_09_02:
                if (opsPreCheckbox0901.isChecked()) {
                    opsPreCheckbox0901.setChecked(false);
                }
                if (opsPreCheckbox0902.isChecked()) {
                    ivDrugFound.setVisibility(View.VISIBLE);
                } else {
                    ivDrugFound.setVisibility(View.GONE);
                }
                break;
            case R.id.sure:
                boolean sign01=false,sign02=false;
                for (int i = 0; i < AppConfig.NURSEBEAN.getObject().getNursingItemList().size(); i++) {
                    if (AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).getItemCode().equals("WJB_Doctor_Sign")) {
                      if(AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).getItemName().length()>0)
                      {
                          sign01=true;
                      }
                    }

                    if (AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).getItemCode().equals("WJB_Nurse_Sign")) {
                        if(AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).getItemName().length()>0)
                        {
                            sign02=true;
                        }
                    }
                }

                if(sign01&&sign02) {
                    insertContent();
                }
                else {
                    dialog = new ClickDialog(aty).builder().setTxt("存在未签名,是否提交？");
                    dialog.isCancleBack(true);
                    dialog.setCallBack(new ClickDialog.ICallBack() {
                        @Override
                        public void callback(Bundle bundle) {
                            if(bundle.getInt("data") == 0){
                                insertContent();
                            } else if(bundle.getInt("data") == 1){
                                dialog.dismiss();
                            }

                        }
                    });
                    dialog.show();
                }
                break;
            case R.id.bt_add_analgesia:
                Intent intent1 = new Intent(this, Act_Nursing_WJB.class);
                Bundle bundle1 = new Bundle();
                bundle1.putInt("type", 1);
                intent1.putExtras(bundle1);
                aty.startActivityForResult(intent1, 1);
                break;
            case R.id.btn_ra_sign_01:
                showTypeDialog();
                type = 1;
                break;
            case R.id.btn_ra_sign_04:
                showTypeDialog();
                type = 4;
                break;
        }
    }

    private void showTypeDialog() {
        ActionSheetDialog actionSheetDialog;
        actionSheetDialog = new ActionSheetDialog(this).builder()
                .setCancelable(true)
                .setCanceledOnTouchOutside(true);
        actionSheetDialog.addSheetItem("HIS签名", ActionSheetDialog.SheetItemColor.Blue,
                new ActionSheetDialog.OnSheetItemClickListener() {
                    @Override
                    public void onClick(int which) {
                        loginDialog.builder();
                        loginDialog.show();
                        loginDialog.setCallBack(new LoginDialog.ICallBack() {
                            @Override
                            public void callback(Bundle bundle) {
                                Map<String, String> params = new HashMap<>();
                                params.put("jobNum", bundle.getString("person_num"));
                                params.put("loginPwd", bundle.getString("person_psw"));
                                presenter_sign.getSignInfo(params);
                            }
                        });
                    }
                });
        actionSheetDialog.addSheetItem("虹膜签名(暂不可用)", ActionSheetDialog.SheetItemColor.Blue,
                new ActionSheetDialog.OnSheetItemClickListener() {
                    @Override
                    public void onClick(int which) {

                    }
                });
        actionSheetDialog.show();
    }
}
